package io.itit.ecp.admin.server.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import icu.mhb.mybatisplus.plugln.base.service.impl.JoinServiceImpl;
import io.itit.ecp.admin.server.entity.DataFillingDataEntity;
import io.itit.ecp.admin.server.mapper.DataFillingDataMapper;
import io.itit.ecp.admin.server.service.IDataFillingDataService;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Objects;


@Service
public class DataFillingDataServiceImpl extends JoinServiceImpl<DataFillingDataMapper, DataFillingDataEntity>
        implements IDataFillingDataService {

    @Override
    public void deleteByDataFillingId(String dataFillingId) {
        getBaseMapper().delete(new LambdaQueryWrapper<DataFillingDataEntity>()
                .eq(DataFillingDataEntity::getDataFillingId, dataFillingId));
    }

    @Override
    public BigDecimal totalCarbonEmissionByDataFillingId(String dataFillingId) {
        DataFillingDataEntity dataEntity = getBaseMapper().selectOne(new QueryWrapper<DataFillingDataEntity>()
                .select("SUM(carbon_Emission) AS carbonEmission")
                .eq("data_filling_id", dataFillingId));
        return Objects.isNull(dataEntity) ? BigDecimal.ZERO : ObjectUtil.defaultIfNull(dataEntity.getCarbonEmission(), BigDecimal.ZERO);
    }

    @Override
    public long countByDataFillingId(String dataFillingId) {
        return count(new LambdaQueryWrapper<DataFillingDataEntity>()
                .eq(DataFillingDataEntity::getDataFillingId, dataFillingId));
    }
}
